<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第一个three课程</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <!-- <canvas width="500" height="500" id="canvas"></canvas> -->
    <script type="module">
        import * as THREE from 'three'
        const scene = new THREE.Scene();
        // scene.background = new THREE.Color(0xff7f50)
        scene.background = new THREE.Color("rgb(200,150,240)")
        //也可以设置背景图片
        // const loader = new THREE.TextureLoader()
        // loader.load('../assets/earth.png', function (texture) {
        //     scene.background = texture
        //     //图片加载耗时，这边是等图片加载完成之后执行的回调，这个时候已经渲染完毕。所以需要再次render。
        //     //但是下面有animate在重复render,所以这边是特殊情况，可以不需要加
        //     renderer.render(scene, camera)
        // })

        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const renderer = new THREE.WebGLRenderer({
            antialias: true //消除锯齿状毛边
        });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        //或者页面有canvas画布，想放在这个里面的话
        // const renderer = new THREE.WebGLRenderer({
        //     canvas: document.getElementById('canvas')
        // });
        camera.position.z = 8
        camera.lookAt(0, 0, 0)

        //添加一个立方体
        const cube = new THREE.Mesh(
            new THREE.BoxGeometry(1, 1, 1),
            new THREE.MeshBasicMaterial({ color: 0x00ff00 })
        );
        const cube2 = new THREE.Mesh(
            new THREE.BoxGeometry(1, 1, 1),
            new THREE.MeshBasicMaterial({ color: 0x00ffee })
        );
        cube2.position.x = 3
        scene.add(cube, cube2)

        function animate() {
            cube.rotation.x += 0.01
            cube.rotation.y += 0.01

            renderer.render(scene, camera)
            requestAnimationFrame(animate)
        }

        animate()
    </script>
</body>

</html>